package com.bdqn.controller;

import com.bdqn.pojo.SysUser;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 第一个Spring MVC控制器
 */
@Controller // 使用@Controller注解标记该类为SpringMVC的控制器
@RequestMapping("/hello")
//public class HelloController extends AbstractController  {
public class HelloController  {

    Logger logger = Logger.getLogger(HelloController.class);

    /**
     * 重写父类方法
     */
//    @Override
//    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        System.out.println("Spring MVC框架搭建成功。");
//        return new ModelAndView("/hello");
//    }

    /**
     * hello Spring MVC
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/hello")
    public String hello()throws  Exception{
        System.out.println("Spring MVC框架搭建成功。");
        // 跳转到hello页面
        return "hello";
    }
    @RequestMapping("/hello1")
    public String hello1() throws Exception {
        logger.info("我的第一个Spring MVC程序，运行成功啦！");
        // 跳转到hello页面
        return "hello";
    }
    @RequestMapping("/hello2")
    public String hello2() throws Exception {
        logger.info("hello控制器下的另一个接口！");
        // 跳转到hello页面
        return "hello";
    }
    @RequestMapping("/hello3")
    public String hello3(@RequestParam String realName)throws Exception{
        logger.info("你好"+ realName +"欢迎来到Spring MVC课堂 ");
        // 跳转到hello页面
        return "hello";
    }
    @RequestMapping("/hello4")
    public String hello4(@RequestParam(required = false) String realName)throws  Exception{
        logger.info("你好"+ realName +"欢迎来到Spring MVC课堂 ");
        // 跳转到hello页面
        return "hello";
    }
    @RequestMapping("/hello5")
    public String hello5(@RequestParam(value = "userName",required = false) String realName)throws  Exception{
        logger.info("你好"+ realName +"欢迎来到Spring MVC课堂 ");
        // 跳转到hello页面
        return "hello";
    }
    @RequestMapping("/hello6")
    public ModelAndView hello6(@RequestParam String realName)throws Exception {
        logger.info("你好【"+ realName +"】欢迎来到Spring MVC课堂 ");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("realName",realName);
        modelAndView.setViewName("hello");
        // 需要跳转的视图名称
        return modelAndView;
    }
    @RequestMapping("/hello7")
    public String hello7(Model model ,@RequestParam String realName)throws Exception{
        logger.info("你好【"+ realName +"】欢迎来到Spring MVC课堂 ");
        model.addAttribute("realName",realName);
        model.addAttribute(realName);
        SysUser sysUser = new SysUser();
        sysUser.setRealName(realName);
        model.addAttribute("currentUser",sysUser);
        model.addAttribute(sysUser);
        return "hello";
    }

    @RequestMapping("/hello8")
    public String hello8(Map<String,Object> map, @RequestParam String realName)throws  Exception{
        logger.info("你好【"+ realName +"】欢迎来到Spring MVC课堂 ");
        map.put("realName",realName);
        return "hello";
    }
}
